<?php
class Jadwal_pelajaran extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('jadwal_model');
		$this->load->model('pelajaran_model');
		$this->load->model('penilaian_model');
	}
	
	function index($act='')
	{
		$data['value_tingkat'] = '';
		$data['value_kelas'] = '';
		
			if($this->input->post('tingkat')!=''){
				$this->session->set_userdata('sess_pg_tingkat', $this->input->post('tingkat'));
			}
			
			if($this->input->post('kelas')!='' ){
				$this->session->set_userdata('sess_pg_kelas', $this->input->post('kelas'));
			}
			
			$data['value_tingkat'] = $this->session->userdata('sess_pg_tingkat');
			$data['value_kelas'] = $this->session->userdata('sess_pg_kelas');
			
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['JAM_KE'] = array('JAM',110,FALSE,'center',1);
		$colModel['SENIN'] = array('SENIN',160,FALSE,'center',1);
		$colModel['SELASA'] = array('SELASA',160,FALSE,'center',1);
		$colModel['RABU'] = array('RABU',160,FALSE,'center',1);
		$colModel['KAMIS'] = array('KAMIS',160,FALSE,'center',1);
		$colModel['JUMAT'] = array('JUMAT',160,FALSE,'center',1);
		$colModel['SABTU'] = array('SABTU',160,FALSE,'center',1);
		$colModel['MINGGU'] = array('MINGGU',160,FALSE,'center',1);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 100,
		'rpOptions' => '[15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Jadwal Pelajaran'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		
		//Build js
		
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$grid_js = build_grid_js('flex1',site_url("/jadwal_pelajaran/listjadwal_grid/".$data['value_tingkat']."/".$data['value_kelas']),$colModel,'no','asc',$gridParams,$buttons);
		else
			$grid_js = build_grid_js('flex1',site_url("/jadwal_pelajaran/listjadwal_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}     
			} 
			
			function popup(url,width,height) 
			{
				 var left   = (screen.width  - width)/2;
				 var top    = (screen.height - height)/2;
				 var params = 'width='+width+', height='+height;
				 params += ', top='+top+', left='+left;
				 params += ', directories=no';
				 params += ', location=no';
				 params += ', menubar=no';
				 params += ', resizable=no';
				 params += ', scrollbars=yes';
				 params += ', status=no';
				 params += ', toolbar=no';
				 newwin=window.open(url,'windowname5', params);
				 if (window.focus) {newwin.focus()}
				 return false;
			}
			
			
			function go_add(id_tk, id_kls, id_jam, id_day){
				popup('".base_url()."index.php/jadwal_pelajaran/input_jadwal_pelajaran/'+id_tk+'/'+id_kls+'/'+id_jam+'/'+id_day,600,510);
			}
			
			function go_edit(id_tk, id_kls, id_jam, id_day, kode_matpel,nip, keterangan,id_jadwal){
				popup('".base_url()."index.php/jadwal_pelajaran/edit_jadwal_pelajaran/'+id_tk+'/'+id_kls+'/'+id_jam+'/'+id_day+'/'+matpel+'/'+nip+'/'+keterangan+'/'+id_jadwal,600,510);
			}
			
			function go_delete(id_jadpel,nama_jadpel){
			
				if(confirm('Anda yakin ingin menghapus jadwal pelajaran ' + nama_jadpel + ' pada jam ini?')){
					$.ajax({
							   type: 'POST',
							   url: '".site_url('/jadwal_pelajaran/delete_jadwal_pelajaran')."',
							   data: 'jadpel='+id_jadpel,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
					});
				}
			}
			
			</script>
			";
		
		$data['tingkat'] = $this->master_model->combo_tingkat_kelas($this->session->userdata('unitid'));
		$data['kelas'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		
		$data['url_filter'] = 'jadwal_pelajaran/index/act_filter';
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listjadwal_grid($id_tingkat='',$id_kelas='')
	{	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('JAM_KE');
		
		$this->flexigrid->validate_post('JAM_KE','asc',$valid_fields);
		
		$records = $this->jadwal_model->get_jadwalpelajaran($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$this->session->userdata('semester'),$id_tingkat,$id_kelas);
		
		$jumlahjadwal = $this->jadwal_model->get_jumlahjadwal2($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'));
		
		$records_jam = $this->jadwal_model->get_jadwalbelajar($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'));
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		
		$tabelpelajaran = array();
		$pegawaipelajaran = array();
		$editdel = array();
		$jadwal_jam = array();
		
		for($x=1;$x<=$jumlahjadwal;$x++){
			$tabelpelajaran[1][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "1")\'>';
			$tabelpelajaran[2][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "2")\'>';
			$tabelpelajaran[3][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "3")\'>';
			$tabelpelajaran[4][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "4")\'>';
			$tabelpelajaran[5][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "5")\'>';
			$tabelpelajaran[6][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "6")\'>';
			$tabelpelajaran[7][$x] = '<img title="Tambah Jadwal !" border=\'0\' src=\''.base_url().'images/add.png\' style=\'cursor:pointer\' onclick=\'go_add("'.$id_tingkat.'","'.$id_kelas.'","'.$x.'", "7")\'>';
			
			$pegawaipelajaran[1][$x] = '';
			$pegawaipelajaran[2][$x] = '';
			$pegawaipelajaran[3][$x] = '';
			$pegawaipelajaran[4][$x] = '';
			$pegawaipelajaran[5][$x] = '';
			$pegawaipelajaran[6][$x] = '';
			$pegawaipelajaran[7][$x] = '';
			
			$editdel[1][$x] = '';
			$editdel[2][$x] = '';
			$editdel[3][$x] = '';
			$editdel[4][$x] = '';
			$editdel[5][$x] = '';
			$editdel[6][$x] = '';
			$editdel[7][$x] = '';
			
			$jadwal_jam[$x] = '';
			
		}
		
		foreach ($records_jam['records']->result() as $row_jam)
		{
			$jam_mulai = split(':',$row_jam->JAM_MULAI);
			$jam_selesai = split(':',$row_jam->JAM_SELESAI);
			$jadwal_jam[$row_jam->JAM_KE] = $jam_mulai[0].':'.$jam_mulai[1].' - '.$jam_selesai[0].':'.$jam_selesai[1];
		}
		
		foreach ($records['records']->result() as $row)
		{
			$nampeg = '';
			$tabelpelajaran[$row->HARI][$row->JAM_KE] = $row->NAMA_MATA_PELAJARAN;
			
			if($row->GELAR_DEPAN !='' && $row->GELAR_BELAKANG !='' )
				$nampeg = $row->GELAR_DEPAN.'. '.$row->NAMA_LENGKAP.', '.$row->GELAR_BELAKANG;
			else if($row->GELAR_DEPAN != '')
				$nampeg = $row->GELAR_DEPAN.'. '.$row->NAMA_LENGKAP;
			else if($row->GELAR_BELAKANG != '')
				$nampeg = $row->NAMA_LENGKAP.', '.$row->GELAR_BELAKANG;
			else $nampeg = $row->NAMA_LENGKAP;
			
			$pegawaipelajaran[$row->HARI][$row->JAM_KE] = $nampeg;
			
			if($pegawaipelajaran != ''){
				$editdel[$row->HARI][$row->JAM_KE] = '<img title="Edit Jadwal !" border=\'0\' src=\''.base_url().'images/edit.png\' style=\'cursor:pointer\' onclick=\'go_edit("'.$row->ID_TINGKAT.'","'.$row->ID_KELAS.'","'.$row->JAM_KE.'", "'.$row->HARI.'", "'.$row->KODE_MATA_PELAJARAN.'", "'.$row->NIP.'", "'.$row->KETERANGAN.'","'.$row->ID_JADWAL_PELAJARAN.'")\'>'.'&nbsp;&nbsp;&nbsp;'.'<img title="Delete Jadwal !" border=\'0\' src=\''.base_url().'images/delete2.png\' style=\'cursor:pointer\' onclick=\'go_delete("'.$row->ID_JADWAL_PELAJARAN.'","'.$row->NAMA_MATA_PELAJARAN.'")\'>';
			}
		}
		for($x=1;$x<=$jumlahjadwal;$x++){
			$record_items[] = array($x,
			'<div align="center">'.$x .'</br>'.$jadwal_jam[$x].'</div>',
			'<div align="center">'.$tabelpelajaran[1][$x].'</br>'.$pegawaipelajaran[1][$x].'</br>'.$editdel[1][$x].'</div>',
			'<div align="center">'.$tabelpelajaran[2][$x].'</br>'.$pegawaipelajaran[2][$x].'</br>'.$editdel[2][$x].'</div>',
			'<div align="center">'.$tabelpelajaran[3][$x].'</br>'.$pegawaipelajaran[3][$x].'</br>'.$editdel[3][$x].'</div>',
			'<div align="center">'.$tabelpelajaran[4][$x].'</br>'.$pegawaipelajaran[4][$x].'</br>'.$editdel[4][$x].'</div>',
			'<div align="center">'.$tabelpelajaran[5][$x].'</br>'.$pegawaipelajaran[5][$x].'</br>'.$editdel[5][$x].'</div>',
			'<div align="center">'.$tabelpelajaran[6][$x].'</br>'.$pegawaipelajaran[6][$x].'</br>'.$editdel[6][$x].'</div>',
			'<div align="center">'.$tabelpelajaran[7][$x].'</br>'.$pegawaipelajaran[7][$x].'</br>'.$editdel[7][$x].'</div>',
			);
		}
		
		if($id_tingkat!='' and $id_kelas!='' )
			$records['record_count'] = $jumlahjadwal*7;
		
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_data($value){
		
		$jamdari = $this->input->post('jamke');
		$jumlahjadwal2 = $this->jadwal_model->get_jumlahjadwal2($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'));
		$id_tingkat = $this->input->post('idtingkat');
		$id_kelas = $this->input->post('idkelas');
		$id_day = $this->input->post('idday');
		if($value>=$jamdari && $value<=$jumlahjadwal2){
			$jamid = $jamdari;
			for($z=$jamdari;$z<=$value;$z++){
				$cekd = $this->jadwal_model->cek_jadwal_exist($id_tingkat, $id_kelas, $id_day,$jamid);
				if($cekd == 1){
					$this->form_validation->set_message('cek_data', ' Jadwal Sudah Ada diantara Jam Awal dan Jam Akhir!!!');
					return FALSE;break;
				}
				$jamid++;
			}
			return TRUE;
		}
		else{
			// echo 'masuk2';exit;
				$this->form_validation->set_message('cek_data', ' %s tidak boleh kurang dari Jam Awal dan  tidak boleh lebih dari jumlah Jadwal Jam Belajar!!!');
				return FALSE;
		}		
		
	}
	
	function cek_guru($value){
		$jamke = $this->input->post('jamke');
		$id_day = $this->input->post('idday');
		$cekguru = $this->jadwal_model->cek_guruajar($jamke, $id_day, $value);
		
		if($cekguru != 1){
			return TRUE;
		}
		else{
			$this->form_validation->set_message('cek_guru', ' %s ini Sudah Mengajar di kelas lain pada jam tersebut!!!');
			return FALSE;
		}		
		
	}
	
	function cek_validasi() {
			// Setting Rules
			$this->form_validation->set_rules('pelajaran', 'Pelajaran', 'required');
			$this->form_validation->set_rules('guru', 'Guru', 'required|callback_cek_guru');
			$this->form_validation->set_rules('jamkesampai', 'Jam Akhir', 'required|numeric|callback_cek_data');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			$this->form_validation->set_message('numeric', 'Kolom %s harus berisi Angka!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function input_jadwal_pelajaran($id_tingkat, $id_kelas, $id_jam, $id_day)
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['idpel'] = $this->input->post('pelajaran');
			$data['guru2'] = $this->input->post('guru');
			$data['jamke'] = $id_jam;
			$data['idday'] = $id_day;
			$data['idtingkat'] = $id_tingkat;
			$data['idkelas'] = $id_kelas;
			$data['pelajaran'] = $this->master_model->combo_pelajaran($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'));
			$data['guru'] = $this->penilaian_model->getGuru();
			$data['kelas'] = $this->jadwal_model->get_namakelas($id_kelas);
			$data['id_kelas'] = $id_kelas;
			$this->load->view('form_jadwal_pelajaran',$data);
		}
		else{
			$this->insert_jadwalpelajaran($id_tingkat, $id_kelas, $id_jam, $id_day);
		}            
    }
	
	function insert_jadwalpelajaran($id_tingkat, $id_kelas, $id_jam, $id_day){
		$jamsampai = $this->input->post('jamkesampai');
		$jamid = $id_jam;
		for($y=$id_jam;$y<=$jamsampai;$y++){
			$data = array(
				'ID_TAHUNAJARAN'		=> $this->session->userdata('tahunajaran'),
				'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
				'ID_SEMESTER'			=> $this->session->userdata('semester'),
				'ID_TINGKAT'			=> $id_tingkat,
				'ID_KELAS'				=> $id_kelas,
				'HARI'					=> $id_day,	
				'NIP'					=> $this->input->post('guru'),
				'KODE_MATA_PELAJARAN'	=> $this->input->post('pelajaran'),	
				'JAM_KE'				=> $jamid,	
				'KETERANGAN'			=> $this->input->post('keterangan')
			);
			
			$this->jadwal_model->insert_jadwalpelajaran($data);
			$jamid++;
		}
		
		echo '<script type="text/javascript">
			  window.opener.location.reload();
			  window.close();
			  </script>';
		
		// $this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		// redirect('jadwal_pelajaran'); 
	}
	
	function edit_jadwal_pelajaran($id_jjb)
    {
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			// $data['param_id'] = $id_tingkat."#".$id_kelas."#".$id_smt;
			
			$data['rs_jadwalbelajar'] = $this->jadwal_model->get_jadwalID($id_jjb);
			$data['content'] = $this->load->view('form_jadwaljambelajar',$data,true);
			$this->load->view('main',$data);
		}
		else{
			// if($this->cek_jenisujian($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('kode_ujian'))->jml > 0 and $this->cek_jenisujian($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('kode_ujian'))->ID_JENIS_UJIAN != $id_ujian){
				// $data['status'] = 'edit';
				// $data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
				// $data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
				// $data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				
				// $data['rs_jenisujian'] = $this->pelajaran_model->get_jenisujianID($id_ujian);
				// $data['err_msg'] = 'Kode Mata Pelajaran telah ada dalam database.';
				// $data['content'] = $this->load->view('form_jenisujian',$data,true);
				// $this->load->view('main',$data);
				
			// }else
				$this->update_jadwal_pelajaran($id_jjb);
		}            
    }
	
	function update_jadwal_pelajaran($id_jjb){
		$data = array(
			'TAHUN_AJARAN'			=> $this->session->userdata('tahunajaran'),
			'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
			'JAM_KE'				=> $this->input->post('jamke'),	
			'JAM_MULAI'				=> $this->input->post('jam_mulai').':'.$this->input->post('menit_mulai'),	
			'JAM_SELESAI'			=> $this->input->post('jam_selesai').':'.$this->input->post('menit_selesai'),	
			'KETERANGAN'			=> $this->input->post('keterangan')
		);
		$this->jadwal_model->update_jadwalbelajar($data, $id_jjb);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('jadwal_pelajaran'); 
	}
	
	//Delete
	function delete_jadwal_pelajaran()
	{
		$id_jadwal_pelajaran = $this->input->post('jadpel');
		$msg = '';
		$this->jadwal_model->delete_jadwal_pelajaran($id_jadwal_pelajaran);
		$msg .= "Data Jadwal Jam Pelajaran berhasil dihapus.\n";
		
			  
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
	
}
?>
